<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false"
    :close-on-press-escape="false">
    <el-col :span="24">
      <el-form v-loading="loading" :model="dataForm" :rules="dataRule" ref="dataForm"
        @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
        <el-col :span="24">
          <el-form-item :label="$t('bpmTaskExtUrl.defType')" prop="defType">
            <!--文本框-->
            <el-input v-model="dataForm.defType" :placeholder="$t('bpmTaskExtUrl.defType')" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('bpmTaskExtUrl.defName')" prop="defName">
            <!--文本框-->
            <el-input v-model="dataForm.defName" :placeholder="$t('bpmTaskExtUrl.defName')" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item prop="url" label="PCURL" class="menu-list">
            <el-popover v-model="menuListVisible" ref="PcMenuListPopover" placement="bottom-start" trigger="click">
              <el-tree :data="menuList | menuListFilters('C01')"
                :props="{ label: 'name', children: 'children' ,disabled : true}" node-key="id" ref="menuListTree"
                :highlight-current="true" :expand-on-click-node="false" accordion
                @current-change="menuListTreeCurrentChangeHandle">
              </el-tree>
            </el-popover>
            <el-input v-model="dataForm.url" v-popover:PcMenuListPopover :readonly="true" placeholder="请选择url地址">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item prop="appUrl" label="APPURL" class="menu-list">
            <el-popover v-model="menuListVisible1" ref="AppMenuListPopover" placement="bottom-start" trigger="click">
              <el-tree :data="menuList | menuListFilters('C02')"
                :props="{ label: 'name', children: 'children' ,disabled : true}" node-key="id" ref="menuListTree"
                :highlight-current="true" :expand-on-click-node="false" accordion
                @current-change="appMenuListTreeCurrentChangeHandle">
              </el-tree>
            </el-popover>
            <el-input v-model="dataForm.appUrl" v-popover:AppMenuListPopover :readonly="true" placeholder="请选择url地址">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <!-- <el-form-item :label="$t('bpmTaskExtUrl.url')" prop="url">
            <el-input v-model="dataForm.url" :placeholder="$t('bpmTaskExtUrl.url')" clearable></el-input>
          </el-form-item> -->
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('bpmTaskExtUrl.className')" prop="className">
            <!--文本框-->
            <el-input v-model="dataForm.className" :placeholder="$t('bpmTaskExtUrl.className')" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('bpmTaskExtUrl.methodsName')" prop="methodsName">
            <!--文本框-->
            <el-input v-model="dataForm.methodsName" :placeholder="$t('bpmTaskExtUrl.methodsName')" clearable>
            </el-input>
          </el-form-item>
        </el-col>
      </el-form>
    </el-col>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" :loading="loading" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
import { treeDataTranslate } from '@/utils/index'

export default {
  data () {
    return {
      loading: false,
      visible: false,
      dataForm: {
        id: '',
        defType: '',
        defName: '',
        menuId: '',
        url: '',
        appUrl: '',
        className: '',
        methodsName: ''
      },
      menuList: [],
      menuListVisible: false,
      menuListVisible1: false

    }
  },
  filters: {
    menuListFilters: function (value, client) {
      let keyData = value.filter(item => item.client === client)
      if (keyData) {
        return keyData
      }
      return {}
    }
  },
  computed: {
    dataRule () {
      return {
        defType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }

        ],
        defName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }

        ],
        className: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }

        ],
        methodsName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }

        ],
        url: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
        this.getMenuList()
      })
    },
    // 获取信息
    getInfo () {
      this.loading = true
      this.$http.get(`/bpm/bpmTaskExtUrl/${this.dataForm.id}`).then(({ data: res }) => {
        this.loading = false
        if (res.code !== 0) {
          return
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => { })
    },
    // 获取菜单列表
    getMenuList () {
      return this.$http.get('/sys/menu/list?type=0').then(({ data: res }) => {
        if (res.code !== 0) {
          return
        }
        this.menuList = treeDataTranslate(res.data, 'id', 'pid')
      }).catch(() => { })
    },
    // 上级菜单树, 选中
    menuListTreeCurrentChangeHandle (data) {
      if (data.url) {
        this.dataForm.url = data.url
        this.dataForm.menuId = data.id
        this.menuListVisible = false
      } else {
        this.$message.error('请选择子节点')
        this.menuListVisible = true
      }
    },
    // 上级菜单树, 选中
    appMenuListTreeCurrentChangeHandle (data) {
      if (data.url) {
        this.dataForm.appUrl = data.url
        this.dataForm.appMenuId = data.id
        this.menuListVisible1 = false
      } else {
        this.$message.error('请选择子节点')
        this.menuListVisible1 = true
      }
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.loading = true
        this.$http[!this.dataForm.id ? 'post' : 'put']('/bpm/bpmTaskExtUrl/', this.dataForm).then(({ data: res }) => {
          this.loading = false
          if (res.code !== 0) {
            return
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => { })
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
